home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / copyrite.cc next >
Encoding:
C/C++ Source or Header  |  1995-12-26  |  2.3 KB  |  102 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dir.h>
  6. #include <glob.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #include <utime.h>
  11.  
  12. int
  13. main(void)
  14. {
  15.   int i;
  16.  
  17.   glob_t flist;
  18.   glob(".../*", 0, 0, &flist);
  19.  
  20.   for (i = 0; i<flist.gl_pathc; i++)
  21.   {
  22.     char pathp[300], fname[100], ext[100];
  23.     fnsplit(flist.gl_pathv[i], 0, pathp, fname, ext);
  24.  
  25.     struct stat st;
  26.     if (stat(flist.gl_pathv[i], &st) < 0)
  27.       continue;
  28.  
  29.     char cline[200];
  30.     struct tm *tm = localtime(&st.st_mtime);
  31.     int y = tm->tm_year;
  32.     if (y<80) y += 2000;
  33.     if (y<200) y += 1900;
  34.  
  35.     // Compute what we'd like the first line to be
  36.     if (!strcmp(ext, ".c")
  37.     || !strcmp(ext, ".h")
  38.     || !strcmp(ext, ".cc")
  39.     || !strcmp(ext, ".y")
  40.     || !strcmp(ext, ".s"))
  41.     {
  42.       sprintf(cline, "/* Copyright (C) %d DJ Delorie, see COPYING.DJ for details */\n", y);
  43.     }
  44.     else if (!strcmp(fname, "makefile") && !strcmp(ext, ""))
  45.     {
  46.       sprintf(cline, "# Copyright (C) %d DJ Delorie, see COPYING.DJ for details\n", y);
  47.     }
  48.     else if (!strcmp(ext, ".asm"))
  49.     {
  50.       sprintf(cline, "; Copyright (C) %d DJ Delorie, see COPYING.DJ for details\n", y);
  51.     }
  52.     else
  53.       continue;
  54.  
  55.     // read first line
  56.     FILE *f = fopen(flist.gl_pathv[i], "r");
  57.     char line1[2000];
  58.     fgets(line1, 2000, f);
  59.  
  60.     // It's OK
  61.     if (strcmp(line1, cline) == 0)
  62.     {
  63.       fclose(f);
  64.       continue;
  65.     }
  66.  
  67.     // Is it copyright someone else?
  68.     if (strstr(line1, "Copyright") && !strstr(line1, "Delorie"))
  69.     {
  70.       printf("%s: %s", flist.gl_pathv[i], line1);
  71.       fclose(f);
  72.       continue;
  73.     }
  74.  
  75.     // We need to append the right copyright notice.
  76.  
  77.     char tmp[300];
  78.     if (pathp[0] == 0)
  79.       strcpy(tmp, "tempxxx.crn");
  80.     else
  81.       sprintf(tmp, "%s/tempxxx.crn", pathp);
  82.     printf("updating %s\n", flist.gl_pathv[i]);
  83.  
  84.     FILE *wf = fopen(tmp, "w");
  85.     fputs(cline, wf);
  86.     fputs(line1, wf);
  87.     while (fgets(line1, 2000, f))
  88.       fputs(line1, wf);
  89.     fclose(wf);
  90.  
  91.     fclose(f);
  92.  
  93.     rename(tmp, flist.gl_pathv[i]);
  94.  
  95.     struct utimbuf ut;
  96.     ut.actime = st.st_atime;
  97.     ut.modtime = st.st_mtime;
  98.     utime(flist.gl_pathv[i], &ut);
  99.   }
  100.   return 0;
  101. }
  102.